<template>
  <div>

    <div class="mg-10">
      <el-input style="width: 200px" suffix-icon="el-icon-search" placeholder="请输入名称" v-model="username"/>
      <el-input class="ml-5" style="width: 200px" suffix-icon="el-icon-message" placeholder="请输入邮箱"
                v-model="email"/>
      <el-input class="ml-5" style="width: 200px" suffix-icon="el-icon-location" placeholder="请输入地址"
                v-model="address"/>
      <el-button class="ml-5" type="primary" @click="load">搜索</el-button>
      <el-button class="ml-5" type="warning" @click="refresh">重置</el-button>
    </div>
    <div class="mg-10">
      <el-button type="primary" icon="el-icon-circle-plus-outline" @click="dialogFormVisible = true">新增
      </el-button>
      <el-popconfirm
          style="margin: 0px 10px"
          confirm-button-text='确认'
          cancel-button-text='取消'
          icon="el-icon-info"
          icon-color="red"
          title="确定要批量删除吗？"
          @confirm="deleteBatch"
      >
        <el-button type="danger" icon="el-icon-remove-outline" slot="reference">批量删除</el-button>
      </el-popconfirm>
      <el-upload action="http://localhost:8080/user/import" :show-file-list="false" accept="xlsx"
                 :on-success="handleExcelImportSuccess" style="display: inline-block">
        <el-button type="primary" icon="el-icon-bottom">导入</el-button>
      </el-upload>
      <el-button type="primary" icon="el-icon-top" @click="exp" style="margin-left: 10px">导出</el-button>
    </div>
    <el-table :data="tableData" border stripe @selection-change="handleSelectionChange"
              :header-cell-style="{background:'#eef1f6',color:'#606266'}">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="id" label="ID" width="60px" v-if="false">
      </el-table-column>
      <el-table-column prop="username" label="用户名" width="auto">
      </el-table-column>
      <el-table-column prop="role" label="角色" width="120px">
      </el-table-column>
      <el-table-column prop="nickname" label="昵称" width="100px">
      </el-table-column>
      <el-table-column prop="email" label="邮箱" width="auto">
      </el-table-column>
      <el-table-column prop="phone" label="手机号" width="auto">
      </el-table-column>
      <el-table-column prop="address" label="地址" width="auto">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200px" align="center">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="success"
              @click="handleEdit(scope.row)">编辑
          </el-button>
          <el-popconfirm
              class="ml-5"
              confirm-button-text='确认'
              cancel-button-text='取消'
              icon="el-icon-info"
              icon-color="red"
              title="确定要删除吗？"
              @confirm="handleDelete(scope.row.id)"
          >
            <el-button
                size="mini"
                type="danger"
                slot="reference"
            >删除
            </el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pd-10">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNum"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>

    <el-dialog title="用户" :visible.sync="dialogFormVisible" width="30%"
               :before-close="dialogCancel">
      <el-form :model="userForm" :rules="userFormRules" ref="userForm" label-width="90px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="userForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="userForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="上传头像" prop="avatarUrl" >
          <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/file/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              accept="png"
              style="position: relative;right: 50px;"
          >
            <img v-if="userForm.avatarUrl" :src="userForm.avatarUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="userForm.role" placeholder="请选择角色">
            <el-option
                v-for="item in roles"
                :key="item.name"
                :label="item.name"
                :value="item.flag">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="userForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="userForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="userForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="userForm.address"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogCancel">取 消</el-button>
        <el-button type="primary" @click="saveUser">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "User",
  data() {
    return {
      tableData: [],
      multipleSelection: [],
      userForm: {},
      userFormRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        nickname: [
          {required: true, message: '请输入昵称', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}
        ],
        phone: [
          {required: true, message: '请输入电话', trigger: 'blur'}
        ],
        address: [
          {required: true, message: '请输入地址', trigger: 'blur'}
        ]
      },
      formLabelWidth: '120px',
      username: "",
      email: "",
      address: "",
      total: 0,
      pageNum: 1,
      pageSize: 5,
      dialogFormVisible: false,
      roles: []
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      axios.get("/user/page", {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          username: this.username,
          email: this.email,
          address: this.address
        }
      }).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
      })

      axios.get("/role").then(res => {
        this.roles = res.data
      })
    },
    saveUser() {
      axios.post("/user/save", this.userForm).then(res => {
        if (res) {
          this.$message.success("用户添加成功！")
        } else {
          this.$message.error("用户添加失败！")
        }
        this.dialogFormVisible = false
        this.userForm = {}
        this.load()
      })
    },
    dialogCancel() {
      this.userForm = {}
      this.dialogFormVisible = false
    },
    handleEdit(row) {
      //防止提前出现变化
      this.userForm = {...row}
      this.dialogFormVisible = true
      console.log(this.userForm)
    },
    handleDelete(row) {
      axios.delete("/user/del/" + row).then(res => {
        if (res) {
          this.$message.success("删除成功")
        } else {
          this.$message.error("删除失败")
        }
        this.load()
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(v => v.id)
      axios.post("/user/del/batch", ids).then(res => {
        if (res) {
          this.$message.success("批量删除成功")
        } else {
          this.$message.error("批量删除失败")
        }
        this.load()
      })
    },
    refresh() {
      this.username = "",
          this.email = "",
          this.address = ""
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.pageNum = pageNum
      this.load()
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    exp() {
      window.open("http://localhost:8080/user/export")
    },
    handleExcelImportSuccess() {
      this.$message.success("导入成功！")
      this.load()
    },
    handleAvatarSuccess(res) {
      console.log(res)
      this.userForm.avatarUrl = res
    }
  }
}
</script>

<style scoped>

</style>